package com.ce.webframework.sec.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletContext;

import com.ce.webframework.sec.model.SecMsgTipsDO;
import com.ce.webframework.sec.service.ISecMsgTipsService;
import com.ce.webframework.sec.util.CommonUtil;
import com.ce.webframework.sec.util.TimerTask;
import com.ce.webframework.sec.vo.MsgGlobal;
import com.ce.webframework.sec.vo.SecMsgTipsVO;
import com.ce.webframework.sec.dao.ISecMsgTipsDao;
import com.sitechasia.webx.core.service.impl.BaseService;
import com.sitechasia.webx.core.utils.base.PopulateUtil;

public class SecMsgTipsServiceImpl extends BaseService implements ISecMsgTipsService {

	private ISecMsgTipsDao secMsgTipsDao;
	private ServletContext servletContext;

	public void setServletContext(ServletContext servletContext) {
		System.out.println("servletContext--->"+servletContext);
		this.servletContext = servletContext;
//		System.out.println("ggggggg--->"+servletContext.getAttribute("msgGlobal"));
	}

	public void setSecMsgTipsDao(ISecMsgTipsDao secMsgTipsDao){
		this.secMsgTipsDao = secMsgTipsDao;
	}

	/**
	 * 功能描述:通过receiverCode查询消息集合
	 * @param receiverCode  接收者用户编码
	 * @return SecMsgTipsDO 集合
	 */
	public List<SecMsgTipsVO> getMsgTipsList(String receiverCode){

		List ls = secMsgTipsDao.getMsgTipsList(receiverCode);
		List<SecMsgTipsVO> voList = new ArrayList<SecMsgTipsVO>();
		for(int i=0;i<ls.size();i++){

			SecMsgTipsVO vo = new SecMsgTipsVO();
			PopulateUtil.populate(ls.get(i), vo);
			voList.add(vo);
		}

		System.out.println("voList---->"+voList);
		return voList;

	}

	/**
	 * 功能描述:新增消息
	 */
	public void addMsgTips(SecMsgTipsVO secMsgTipsVO){
		SecMsgTipsDO secMsgTipsDO = new SecMsgTipsDO();
		PopulateUtil.populate(secMsgTipsVO, secMsgTipsDO);
		this.secMsgTipsDao.addMsgTips(secMsgTipsDO);
		if(secMsgTipsDO.getRemindTime() != null)
			CommonUtil.addMsgTimer(servletContext, this, "process", secMsgTipsVO);
		CommonUtil.addMsgGlobal(servletContext, secMsgTipsVO.getReceiverCode());

	}

	/**
	 * 功能描述:系统启动时查询所有未读消息的receiverCode
	 * @return String receiverCode集合
	 */
	public List<String> getCodesNoReadMsg(){
		return secMsgTipsDao.getCodesNoReadMsg();
	}

	/**
	 * 功能描述:更新消息提醒读标志
	 * @param ids  消息纪录id主键集合
	 * @param readFlag 读标志
	 * @return SecMsgTipsDO 集合
	 */
	public void updateReadFlag(List<Long> ids, Character readFlag){
		secMsgTipsDao.updateReadFlag(ids, readFlag);
	}

	/**
	 * 功能描述:查询今天需要初始化的定时器
	 * @return SecMsgTipsVO 集合
	 */
	public List<SecMsgTipsVO> getMsgTipForTimer(){
		return secMsgTipsDao.getMsgTipForTimer();
	}

	/**
	 * 功能描述:删除消息纪录
	 * @param secMsgTipsVO
	 */
	public void deleteMsgTips(SecMsgTipsVO secMsgTipsVO){
		SecMsgTipsDO secMsgTipsDO = new SecMsgTipsDO();
		PopulateUtil.populate(secMsgTipsVO, secMsgTipsDO);
		secMsgTipsDao.deleteMsgTips(secMsgTipsDO);

		CommonUtil.removeMsgTimer(servletContext, secMsgTipsVO.getMsgTipId());

	}

	/**
	 * 功能描述:更新消息纪录
	 * @param secMsgTipsVO
	 */
	public void updateMsgTips(SecMsgTipsVO secMsgTipsVO){
		SecMsgTipsDO secMsgTipsDO = new SecMsgTipsDO();
		PopulateUtil.populate(secMsgTipsVO, secMsgTipsDO);
		secMsgTipsDao.updateMsgTips(secMsgTipsDO);
		CommonUtil.removeMsgTimer(servletContext, secMsgTipsVO.getMsgTipId());
		CommonUtil.addMsgTimer(servletContext, this,"process", secMsgTipsVO);
	}

	/**
	 * 功能描述:定时处理方法
	 * @param vo
	 */
	public void process(SecMsgTipsVO vo){
		System.out.println("process---> "+vo);
		System.out.println("process---> "+vo.getMsgTipId());
		System.out.println("process---> "+vo.getReceiverCode());
//		HashMap<String, TimerTask> map = (HashMap<String, TimerTask>)context.getAttribute("dailyTimers");
		List<Long> ids = new ArrayList<Long>();
		ids.add(vo.getMsgTipId());
		updateReadFlag(ids, '0');//更新为未读

		CommonUtil.addMsgGlobal(servletContext, vo.getReceiverCode());
	}
}
